<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="设备名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
				<el-dropdown :hide-on-click="false">
                    <el-button class="mr10" plain>操作</el-button>
					<template #dropdown>
						<el-dropdown-menu>
							<el-dropdown-item type="text" icon="el-icon-lx-roundcheck" @click="func_handleBatchEditStatus(2001)" class="green"> 批量启用</el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
            </div>
            <el-table :data="memberTableData" border class="table" header-cell-class-name="table-header" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="id" label="ID" width="55" align="center" />
                <el-table-column prop="username" label="账户" />
				<el-table-column prop="nickname" label="用户别称" />
                <el-table-column prop="phone" label="手机号码" />
				<el-table-column prop="integel" label="积分" />
				<el-table-column prop="gradeName" label="设备SN码" />
                <el-table-column prop="strOpenvip" label="是否VIP会员" />
				<el-table-column prop="strStatus" label="状态" />
				<el-table-column prop="createTime" label="注册时间" />
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button class="mr10 red" type="text" icon="el-icon-edit" @click="func_handleDel(scope.$index, scope.row)">删除</el-button>
                        <el-dropdown :hide-on-click="false">
                            <span class="el-dropdown-link green mr10"><el-icon class="el-icon-lx-info mr10" />更多</span>
                            <template #dropdown>
                                <el-dropdown-menu>
                            		<el-dropdown-item type="text" @click="func_changeRowStatus(scope.$index, scope.row, 2001)" class="green"> 启用 </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="dataTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑会员" v-model="editDialogVisible" width="55%" :close-on-click-modal="false">
            <VMember v-bind:selectValue="VMemberData" @MemberData="func_loadVMemberData" />
        </el-dialog>
    </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { findMemberListPageByLikeNickName, batchEditMemberStatus, deleteMember, deleteMemberById, } from "../../../api/hairmanager/member";
import { NO_DATA_SELECT_ERROR, SUCCESS_ADDDATA_DIALOG_MESSAGE, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE, formLabelWidth, } from "../../../api/CommonErrorMessage.js";

import VMember from "./components/memberForm.vue";

export default {
    name: "memberFixTable",
    components: {VMember, },
    setup() {
        const router = useRouter();
		const route = useRoute();
        const formLabelWidth = ref("170px");
        const optColumnWidth = 150;
        const query = reactive({
            keyword: "",
            isStatus: 2002,
            pageNum: 1,
            pageSize: 10,
        });
        const memberTableData = ref([]);
        const dataTotal = ref(0);
        const multipleSelection = ref([]);
        const VMemberData = reactive({
            load: false,
            id: 0,
        });
        let idx = -1;
        // =========================================================================================================
        // 主业务处理 ========================================================================================= START
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findMemberListPageByLikeNickName(query).then((res) => {
                memberTableData.value = res.data.list;
                dataTotal.value = res.data.total;
            });
        };
        func_getData();
        /**
         * @name 主表格查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const handlePageChange = (val) => {
            query.pageNum = val;
            func_getData();
        };
        /**
         * @name 删除操作
         */
        const func_changeRowStatus = (index, row, isStatus) => {
			if(isStatus === 2002) {
				// 二次确认删除
				ElMessageBox.confirm("确定要停用用户吗？", "提示", {
				type: "warning",
				}).then(() => {
					let obj = {ids: [row['id']], isStatus: isStatus};
					func_editMemberStatus(obj);
				}).catch(() => {});
			} else {
				let obj = {ids: [row['id']], isStatus: isStatus};
				func_editMemberStatus(obj);
			}
        };

		const func_editMemberStatus = (obj) => {
			batchEditMemberStatus(obj).then((res) => {
				if(res.code != 200) {
					if(res.type === null){
						ElMessage.error(res.message);
					}else if(res.type === 0) {
						ElMessage.warning(res.message);
					} else {
						ElMessage.error(res.message);
					}
				} else {
					ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
					func_getData();
				}
			});
		}
        const handleSelectionChange = (val) => {
            let ids = [];
            val.forEach((element) => {
                ids.push(element['id']);
            });
            multipleSelection.value = ids;
        };
        /**
         * @name 批次删除数据
         */
        const func_handleBatchEditStatus = (isStatus) => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
			if(isStatus === 2002) {
				// 二次确认删除
				ElMessageBox.confirm("确定要停用用户吗？", "提示", {
				type: "warning",
				}).then(() => {
					let obj = {ids: multipleSelection.value, isStatus: isStatus};
					func_editMemberStatus(obj);
				});
			} else {
				let obj = {ids: multipleSelection.value, isStatus: isStatus};
				func_editMemberStatus(obj);
			}
        }
        const func_handleDel = (index, row) => {
				// 二次确认删除
				ElMessageBox.confirm("确定要删除用户吗？", "提示", {
				type: "warning",
				}).then(() => {
					deleteMemberById(row['id']).then((res) => {
                        if(res.code != 200) {
                            if(res.type === null){
                                ElMessage.error(res.message);
                            }else if(res.type === 0) {
                                ElMessage.warning(res.message);
                            } else {
                                ElMessage.error(res.message);
                            }
                        } else {
                            ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                            func_getData();
                        }
                    });
				});
        };
        // 主业务处理 ========================================================================================= END
		// 扩展业务处理 ========================================================================================= START
		// 表格编辑时弹窗和保存
        const editDialogVisible = ref(false);
        const func_loadVMemberData = (v) => {
            if(v['state'] === 200){
                if(v['editFlg']){
                    ElMessage.success(`修改第 ${idx + 1} 行成功`);
                }
                editDialogVisible.value = false;
                VMemberData['load'] = null;
                func_getData();
            } else if(v['state'] === 1){
                editDialogVisible.value = false;
                VMemberData['load'] = null;
                func_getData();
            } else {
                editDialogVisible.value = true;
            }
        };
		// 扩展业务处理 ========================================================================================= END
        // =========================================================================================================
        return {
            // 系统变量
            optColumnWidth, formLabelWidth, query, memberTableData, dataTotal, multipleSelection, editDialogVisible,
            // 主表格调用方法
            func_handleSearch, handlePageChange, func_changeRowStatus, func_handleDel, func_handleBatchEditStatus,
			func_editMemberStatus,
            handleSelectionChange,
            // 插件业务调用方法
			VMemberData, func_loadVMemberData,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #67c23a;
}
.blue {
    color: #1684FC;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>